/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.example.app.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.BaseActivity;
import com.example.app.R;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年8月14日 上午11:13:19
 * @Title <B>todo</B>
 * 
 *        <pre>
 * todo
 * </pre>
 */

public class TextSwitchUsage extends BaseActivity implements
		ViewSwitcher.ViewFactory {
	TextSwitcher switcher;

	Button previous, next;

	String[] resource = { "HelloAndroid", "eoe.android", "Johnny.Griffin",
			"Gryphone" };

	int id = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.textswitchusage_main);

		initiaView();

	}

	private void initiaView() {
		previous = (Button) findViewById(R.id.previousButton);
		next = (Button) findViewById(R.id.nextButton);

		switcher = (TextSwitcher) findViewById(R.id.switcher);
		switcher.setFactory(this);

		switcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		previous.setOnClickListener(listener);
		next.setOnClickListener(listener);
	}

	private int previousOK(int i) {
		int aint = i;

		if (aint <= 0) {
			aint = aint + resource.length;
		}
		aint = aint - 1;
		return aint;

	}

	private int nextOK(int i) {
		int aint = i + 1;

		if (aint > resource.length - 1) {
			aint = aint - resource.length;
		}

		return aint;
	}

	private OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.previousButton:
				id = previousOK(id);
				switcher.setText(resource[id]);
				break;

			case R.id.nextButton:
				id = nextOK(id);
				switcher.setText(resource[id]);
				break;
			}// switch
		}// onClick
	};// OnClickListener

	@Override
	// the first View
	public View makeView() {
		// TODO Auto-generated method stub
		TextView tv = new TextView(this);
		tv.setText("Heello to TextSwitcher!");

		return tv;
	}
}
